package com.enjoy.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class FailFastTest {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("dd");
        list.add("ee");
//        for (String s : list) {
//            list.add("ff");
//        }

        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println("iterator.next() = " + next);
            if ("bb".equals(next)) {
                iterator.remove();
            }
        }
        System.out.println("list = " + list);

        ListIterator<String> listIterator = list.listIterator();

        while (listIterator.hasNext()) {
            String next = listIterator.next();
            System.out.println("next = " + next);
            if ("cc".equals(next)) {
                listIterator.add("ff");
            }
        }
        System.out.println("list = " + list);
    }
}
